package com.baiyang.app.ui.read.commentList.addComment;

import android.content.Context;

import com.alibaba.fastjson.JSONObject;
import com.baiyang.app.MyApp;
import com.baiyang.app.model.User;
import com.baiyang.app.tool.LogUtils;
import com.baiyang.app.tool.net.SendMsgUtil;
import com.baiyang.app.tool.net.Urls;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Response;

public class AddCommentService {

    private Context context;
    private AddCommentViewModel viewModel;

    public AddCommentService(Context context,AddCommentViewModel viewModel){
        this.context = context;
        this.viewModel = viewModel;
    }


    /**
     * 添加评论
     * @param rat
     * @param comment
     * @param articleId
     */
    public void addComment(float rat, String comment, String articleId) {
        JSONObject object = new JSONObject();
        object.put("score",rat);
        User user = MyApp.getInstance().getUser();
        object.put("userId",user.getId());
        object.put("commentContent",comment);
        object.put("articleId",articleId);
        new SendMsgUtil(){
            @Override
            public void deal(Call call, Response response) throws IOException {
                if(response.code() == 200){
                    String str = response.body().string();
                    LogUtils.i("addComment",str);
                    viewModel.setAddResult(JSONObject.parseObject(str));
                }else{
                    LogUtils.i("addComment","code:" + response.code());
                }
            }
            @Override
            public void dealFailure(Call call, IOException e) throws IOException {
                LogUtils.i("addComment","IOException:" + e.getLocalizedMessage());
            }
        }.sendPost(Urls.COMMENT.ADD_COMMENT,object);
    }
}
